# 使用 CUDA 11.8 基础镜像
FROM nvidia/cuda:11.8.0-runtime-ubuntu22.04

# 使用清华源加速apt安装
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    apt-get update && apt-get install -y \
    wget \
    build-essential \
    software-properties-common \
    libmagic-dev \
    libgl1-mesa-glx \
    && rm -rf /var/lib/apt/lists/*

# 下载并安装Miniconda（使用清华源）
RUN wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /tmp/miniconda.sh && \
    bash /tmp/miniconda.sh -b -p /opt/conda && \
    rm /tmp/miniconda.sh

# 设置环境变量
ENV PATH=/opt/conda/bin:$PATH

# 配置conda仅使用清华源
RUN conda config --remove-key channels && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ && \
    conda config --set show_channel_urls yes && \
    conda config --set ssl_verify false

# 创建conda环境并安装依赖
RUN conda create -n app python=3.11 -y
ENV PATH=/opt/conda/envs/app/bin:$PATH

# 激活环境并安装faiss-gpu（仅使用清华源）
RUN conda install -n app -y -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ faiss-gpu cudatoolkit=11.8

# 创建必要目录
RUN mkdir -p /app/models /app/data && \
    chmod -R 777 /app

# 切换到工作目录
WORKDIR /app

# 复制requirements.txt
COPY requirements.txt /app/

# 安装其他Python依赖
RUN pip --default-timeout=100 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ \
    --trusted-host pypi.tuna.tsinghua.edu.cn -r requirements.txt

# 添加其余文件到app目录下
# COPY . /app/

# 确保所有文件权限正确
RUN chmod -R 777 /app

EXPOSE 6005

# VOLUME /app
# VOLUME /app/data
# VOLUME /app/models